﻿using WRJClient.DAL;
using WRJClient.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using WRJClient.UI.Map;

namespace WRJClient.UI.OverallState
{
    public class ConsoleWindowController
    {
        private ConsoleWindow _consoleWindow = null;
        public ConsoleWindowController(ConsoleWindow consoleWindow)
        {
            _consoleWindow = consoleWindow;
        }

        public void CommandItemSelect(string name, bool? isSelect = null)
        {
            switch (name)
            {
                case "ShowLoginButton":
                    _consoleWindow?.ShowLoginWindow();
                    break;
                case "ShowViewToggleButton":
                    setMainWindowShow(isSelect == true ? true : false);
                    MapManager.Instance.SetViewPoint(0);
                    break;
                case "FullScreenToggleButton":
                    setMainWindowFullScreen(isSelect == true ? true : false);
                    break;
                default:
                    break;
            }
        }

        private void setMainWindowShow(bool show)
        {
            _consoleWindow.MainWindow.WindowState = WindowState.Normal;
            if (show)
            {
                //从另一个显示器弹出
                if (Screen.AllScreens.Count() > 1)
                {
                    Screen s1 = Screen.AllScreens[0];
                    Screen s2 = Screen.AllScreens[1];
                    if (_consoleWindow.Left + (_consoleWindow.ActualWidth / 2) >= s1.WorkingArea.Left && _consoleWindow.Left + (_consoleWindow.ActualWidth / 2) < s1.WorkingArea.Right)
                    {

                        // 第二个显示器显示
                        _consoleWindow.MainWindow.Top = s2.WorkingArea.Top;
                        _consoleWindow.MainWindow.Left = s2.WorkingArea.Left;
                        _consoleWindow.MainWindow.Width = s2.WorkingArea.Width;
                        _consoleWindow.MainWindow.Height = s2.WorkingArea.Height;

                    }
                    else// if (_consoleWindow.Left + _consoleWindow.ActualWidth >= s2.WorkingArea.Left && _consoleWindow.Left + _consoleWindow.ActualWidth < s2.WorkingArea.Right)
                    {
                        // 第一个显示器显示
                        _consoleWindow.MainWindow.Top = s1.WorkingArea.Top;
                        _consoleWindow.MainWindow.Left = s1.WorkingArea.Left;
                        _consoleWindow.MainWindow.Width = s1.WorkingArea.Width;
                        _consoleWindow.MainWindow.Height = s1.WorkingArea.Height;
                    }
                }
                _consoleWindow.MainWindow.mapControl.InitBaseMap();
                _consoleWindow.MainWindow.Show();
                _consoleWindow.MainWindow.WindowState = WindowState.Maximized;
            }
            else
            {
                _consoleWindow.MainWindow.Hide();
            }
        }
        private void setMainWindowFullScreen(bool full)
        {
            if (full)
            {
                _consoleWindow.MainWindow.WindowStyle = WindowStyle.None;
                _consoleWindow.MainWindow.WindowState = WindowState.Maximized;
            }
            else
            {
                _consoleWindow.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                _consoleWindow.MainWindow.WindowState = WindowState.Normal;
            }
        }
    }
}
